<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="drawerWidth"
      :destroyOnClose="true"
      @cancel="handleClose"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="onSubmit"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="onSubmit" type="primary">确认</a-button>
        </div>
      </template>
      <div
        :style="{
          width: '100%',
          border: '1px solid #e9e9e9',
          padding: '10px 16px',
        }"
      >
        <a-spin :spinning="confirmLoading">
          <a-form-model ref="ruleForm" :model="model" :rules="rules">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="班次名称"
              hasFeedback
              prop="teamName"
            >
              <a-input
                placeholder="请输入班次名称"
                v-model.trim="model.teamName"
              />
            </a-form-model-item>

            <a-form-model-item
              label="班次开始时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="startTime"
            >
              <a-time-picker
                style="width:100%"
                placeholder="请选择开始时间"
                format="HH:mm:ss"
                v-model.trim="startTime"
                @change="
                  (time, timeString) =>
                    handleChange(time, timeString, 'startTime')
                "
              />
            </a-form-model-item>
            <a-form-model-item
              label="班次结束时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="endTime"
            >
              <a-time-picker
                style="width:100%"
                v-model.trim="endTime"
                placeholder="请选择结束时间"
                format="HH:mm:ss"
                @change="
                  (time, timeString) =>
                    handleChange(time, timeString, 'endTime')
                "
              />
            </a-form-model-item>
          </a-form-model>
        </a-spin>
      </div>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import moment from "moment";
export default {
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      drawerWidth: 600, // 抽屉宽度
      model: {},
      show: true, //根据菜单类型，动态显示隐藏表单元素
      startTime: "",
      endTime: "",
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        teamName: [{ required: true, message: "请输入班次名称" }],
        startTime: [{ required: true, message: "请选择开始时间" }],
        endTime: [{ required: true, message: "请选择结束时间" }],
      },
    };
  },
  computed: {},
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit(this.model);
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.model = Object.assign({}, record);
      if (this.model.startTime) {
        this.startTime = moment(this.model.startTime, "HH:mm:ss");
      }
      if (this.model.endTime) {
        this.endTime = moment(this.model.endTime, "HH:mm:ss");
      }
    },
    moment,
    range(start, end) {
      const result = [];
      for (let i = start; i < end; i++) {
        result.push(i);
      }
      return result;
    },
    handleChange(time, timeString, key) {
      this.$set(this.model, key, timeString);
    },

    // 关闭
    handleClose() {
      this.visible = false;
      this.model = Object.assign({});
      this.startTime = "";
      this.endTime = "";
      this.$refs.ruleForm.resetFields();
    },

    onSubmit() {
      const that = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.handleSubmit();
        } else {
          return false;
        }
      });
    },
    async handleSubmit() {
      let res;
      this.confirmLoading = true;
      if (this.model.id) {
        res = await api.shiftEdit(this.model); //修改
      } else {
        res = await api.shiftAdd(this.model); //新增
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
      this.model = Object.assign({});
      this.startTime = "";
      this.endTime = "";
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth;
      } else {
        this.drawerWidth = 600;
      }
    },
  },
};
</script>

<style></style>
